<?php
namespace app\common\config\logic;


use app\common\BaseLogic;
use app\common\config\model\ConfigRegion;

class RegionLogic extends BaseLogic
{
    public function getRegion($id, $fields="*")
    {
        $cacheName = "config:region:{$id}";
        if ($fields == "*") {
            $data = $this->redis->hGetAll($cacheName);
        } else {
            $data = $this->redis->hMGet($cacheName, $fields);
        }


        if (empty($data['id'])) {
            $model = new ConfigRegion();
            $row = $model->where('id', $id)->find();
            if (!empty($row)) {
                $data = $row->getData();
                $this->redis->hMSet($cacheName, $data);
                if ($fields != "*") $data = array_search_keys($data, $fields);
            } else {
                $data = [];
            }
        }

        return $data;
    }

    public function resetRegionCache($id) {
        $cacheName = "config:region:{$id}";
        $this->redis->del($cacheName);
    }
}